package com.hope;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 水印添加
 * @author penghui.li
 *
 */
public class ImageBuilderTool {

    public static void main(String[] args) {
        try {
            String srcFile = "d:/1.JPG";
            String descFile = "d:/2.jpg";
            String markFile = "d:/3.jpg";
            File file = new File(srcFile);
            BufferedImage originImg = ImageIO.read(file);
            int allMaxWidth = originImg.getWidth();
            int allMaxHeight = originImg.getHeight();
            int x = 100;
            int y = 100;

            rebuildImage(x, y, srcFile, descFile, allMaxWidth, allMaxHeight, true, null, markFile);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    
    /**
     * 
     * @param x x坐标
     * @param y y坐标
     * @param srcFile 母图片路径
     * @param destFile 生成图片路径
     * @param allMaxWidth 图片最大宽度
     * @param allMaxHeight 图片最大高度 
     * @param isCreateMark 是否创建水印
     * @param contentMark 文字内容
     * @param imageMarkFilePath 水印图片地址
     * @return
     */
    public static boolean rebuildImage(int x,int y,String srcFile, String destFile,
            int allMaxWidth, int allMaxHeight,boolean isCreateMark,String contentMark,String imageMarkFilePath) {
        ScaleImage is = new ScaleImage();
        
       
        try {
            File sourceFile = new File(srcFile);
            is.saveImageAsJpg(sourceFile.getAbsolutePath(), destFile, allMaxWidth,allMaxHeight);
           
            if(isCreateMark){
                File destObj = new File(destFile);
                if(contentMark!=null && contentMark.length()>0){
                    createMarkStr(destFile, contentMark);
                }else if(imageMarkFilePath!=null && imageMarkFilePath.length()>0){
                    createPicMark(destObj,imageMarkFilePath,x,y);
                }
                
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("生成缩略图失败!"+e.getMessage());
            return false;
        }
        

    }
    
    public static void createPicMark(File destFile, String markPicPath,int x,int y) throws IOException {
        BufferedImage  originImg   =   ImageIO.read(destFile); 
        int width1=originImg.getWidth();
        int height1=originImg.getHeight();
        Graphics2D   g2d   =   originImg.createGraphics();   
        g2d.drawImage(originImg,0,0,width1,height1,null);
        BufferedImage  waterMarkImg   =  ImageIO.read(new   File(markPicPath));
        int width2 = waterMarkImg.getWidth(); // 得到水印图宽
        int height2 = waterMarkImg.getHeight(); // 得到水印图长  
        g2d.drawImage(waterMarkImg,x,y,width2,height2,null);
        g2d.dispose();
        FileOutputStream out=new FileOutputStream(destFile);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
        encoder.encode(originImg);
        out.close();
    }

    
    
    public static boolean createMarkStr(String filePath,String strmark) {
        ImageIcon imgIcon = new ImageIcon(filePath);
        Image theImg = imgIcon.getImage();

        int width = theImg.getWidth(null);
        int height = theImg.getHeight(null);
        BufferedImage bimage = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bimage.createGraphics();
        //设置水印字体
        Font font1 = null;
        font1 = new Font("宋体", Font.PLAIN, 12);
        g.setFont(font1);
        g.setColor(Color.WHITE);
        g.drawImage(theImg, 0, 0, null);
        g.drawImage(theImg, width, height, null);

        if(width>140)
          g.drawString(strmark,width-150, height-8); //添加文字
        else if(width>20)
          g.drawString("li511930751",5, height-15); //添加文字
        g.dispose();
        try {
          FileOutputStream out = new FileOutputStream(filePath);
          JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
          JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
          param.setQuality(50f, true);
          encoder.encode(bimage, param);
          out.close();
        }
        catch (Exception e) {
          return false;
        }
        return true;
      }
    
   

}
